package com.dllo.lanou.cookapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dllo.lanou.cookapp.BaseApplication;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.User;

import java.util.HashMap;

import javax.xml.namespace.QName;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;

public class OneKeyLoginActivty extends AppCompatActivity implements View.OnClickListener, PlatformActionListener {

    @Bind(R.id.qqLogin)
    TextView qqlogin;
    @Bind(R.id.sinaLogin)
    TextView sinalogin;
    @Bind(R.id.tv_register)
    TextView tv_register;
    @Bind(R.id.et_username)
    EditText et_username;
    @Bind(R.id.et_password)
    EditText et_password;
    @Bind(R.id.bt_login)
    Button bt_login;
    @Bind(R.id.tv_forgetPwd)
    TextView tv_forgetPwd;
    @Bind(R.id.iv_back)
    ImageView iv_back;
    private String usernameString, pwdString;

    private Handler handler;
    private static final int MSG_SMSSDK_CALLBACK = 1;
    private static final int MSG_AUTH_CANCEL = 2;
    private static final int MSG_AUTH_ERROR= 3;
    private static final int MSG_AUTH_COMPLETE = 4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_key_login_activty);
        ButterKnife.bind(this);
        init();
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 12){
                    HashMap hashMap = (HashMap) msg.obj;
                  //  String qqName = (String) hashMap.get("nickname");
                   // String sinaName = (String) hashMap.get("username");
                  //  if ("nickname".equals(qqName)){
                       // Log.i("xijinhua",qqName);
                   // }
                   // Log.i("xijinhua",name);
                }


            }
        };

    }

    private void init() {

        handler = new Handler();
        qqlogin.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        bt_login.setOnClickListener(this);
        tv_forgetPwd.setOnClickListener(this);
        iv_back.setOnClickListener(this);




    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.qqLogin:
                //QQ空间
                Log.i("lalal", "dianji qq");
                Platform qzone = ShareSDK.getPlatform(QZone.NAME);

                authorize(qzone);



                break;

            case R.id.sinaLogin:
                //新浪微博
                Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);

                authorize(sina);
                break;

            case R.id.tv_forgetPwd:
                //忘记密码

                break;

            case R.id.bt_login:
                //登陆
                usernameString = et_username.getText().toString();
                pwdString = et_password.getText().toString();
                User.loginByAccount(this, usernameString, pwdString, new LogInListener<User>() {
                    @Override
                    public void done(User user, BmobException e) {
                        if (user != null){
                            Toast.makeText(OneKeyLoginActivty.this,"登陆成功",Toast.LENGTH_LONG).show();
                            BaseApplication.setIsLogin(true);
                            BaseApplication.instance.setUser(user);

                            Intent intent = new Intent();
                            setResult(2,intent);
                            finish();
                        }
                    }
                });
                break;

            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.tv_register:
                 //进入注册页面
                Intent intent = new Intent(this,RegisterActivity.class);

                startActivityForResult(intent,1);
                break;
        }
    }

    private void authorize(Platform plat) {
        if (plat == null) {
          //  popupOthers();
            return;
        }
         plat.setPlatformActionListener(this);

        //关闭SSO授权
        plat.SSOSetting(true);

        plat.showUser(null);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        if (i == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = 12;
            msg.obj = new Object[] {platform.getName(), hashMap};
            Log.i("xijinhua",hashMap+"");
            handler.sendMessage(msg);
        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        if (i == Platform.ACTION_USER_INFOR) {
            handler.sendEmptyMessage(MSG_AUTH_ERROR);
        }
        throwable.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int i) {
        if (i == Platform.ACTION_USER_INFOR) {
            handler.sendEmptyMessage(MSG_AUTH_CANCEL);
        }
    }

    //注册之后返回的值

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1&& data != null){
        et_username.setText(data.getStringExtra("username"));
        et_password.setText(data.getStringExtra("pwd"));
        }
    }
}
